package ssinfis.project.kontrolarozvrhu;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.util.Log;

import ssinfis.project.parser.Hodina;
import ssinfis.project.parser.Parser;


public class HttpConnection {
	HttpClient client = new DefaultHttpClient();

	public void httpGetRozvrh() throws Exception {
		BufferedReader in = null;
		try {
			
			HttpGet request = new HttpGet();
			request.setURI(new URI(
					"http://bakalari.ssinfis.cz/prehled.aspx?s=6"));
			HttpResponse response = client.execute(request);
			in = new BufferedReader(new InputStreamReader(response.getEntity()
					.getContent()));
			Parser p = new Parser(in);
//			Hodina[][] rozvrh = p.parse();
//			
//			Log.i("ROZVRH", rozvrh.length+"");
			
			
//			StringBuffer sb = new StringBuffer("");
//			String line = "";
//			String NL = System.getProperty("line.separator");
//			while ((line = in.readLine()) != null) {
//				sb.append(line + NL);
//			}
//			in.close();
//			String page = sb.toString();
//			System.out.println(page);
//			System.out.println(sb);
//			System.out.println(NL);
//			System.out.println(in);

			
			
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	
	
	public String executeHttpPost(String id, String heslo) throws Exception {

		BufferedReader in = null;
		try {

			HttpPost request = new HttpPost(
					"http://bakalari.ssinfis.cz/login.aspx");
			List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
			postParameters.add(new BasicNameValuePair(
					"ctl00$cphmain$TextBoxjmeno", id));
			postParameters.add(new BasicNameValuePair(
					"ctl00$cphmain$TextBoxheslo", heslo));
			UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
					postParameters);
			request.setEntity(formEntity);
			HttpResponse response = client.execute(request);
			in = new BufferedReader(new InputStreamReader(response.getEntity()
					.getContent()));
			StringBuffer sb = new StringBuffer("");
			String line = "";
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
			in.close();
			String result = sb.toString();
			return result;
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
